import { ScrollableContainer } from './../home/scrollable-container';
import { HostBinding } from '@angular/core';
import { Order, Orderitem } from './../../models/order';
import { OrderMessageHandle } from './../../utils/messageHandle/orderMessageHandle';
import { Subscription } from 'rxjs';
import { MessageBoxService, GrowlMessageType } from './../../components/messageBox';
import { MessageHandleManager } from './../../utils/messageHandle/messageHandleManager';
import { FNCManager } from './../../utils/fnc/fncManager';
import { StoreSvr } from './../../services/store';
import { AppStore, System, PagingInfo } from './../../services/appStore';
import { NavigateService } from './../../services/navigate';
import { Component, Directive, ElementRef, OnInit, ChangeDetectorRef, ViewChild } from '@angular/core';



@Component({
    styleUrls: ['./index.scss'],
    selector: 'finished-orders',  // <home></home>
    template: ` 
        <scrollable-container [ordersArray]="orders" class="finishedorders-container">
            <finishedorder-panel *ngFor="let order of orders;trackBy:trackByOrderId" [order]="order"></finishedorder-panel>
        </scrollable-container>
        <img [hidden]="!(orders&&orders.length==0)" src="./assets/img/kds-noorders.png" class="divtcvma" style="transform: translate(-50%, -50%) scale(0.75);top:45%;"/>
    `,
    providers: []
})

export class FinishedOrders implements OnInit {
  @ViewChild(ScrollableContainer)
  private scrollableContainer: ScrollableContainer;
    private orderMessageHandle: OrderMessageHandle
    private orders: Order[];
    private subscribetion: Subscription;
    // TypeScript public modifiers
    @HostBinding('class.rel') layoutColoum: boolean = true;
    constructor(private element: ElementRef, private appStore: AppStore, private changeDetectorRef: ChangeDetectorRef) {
    }

    ngOnInit() {
        this.subscribetion = this.appStore.orderStore.finishedOrders.subscribe(orders => {
            this.orders = orders;
            //排序 根据完成时间
            this.orders.forEach(v => {
                v.finishTime = Math.max(...v.items.filter(x => x.isLocalStall && x.deliverTime).map(x => x.deliverTime))
            })

            this.orders.sort((x, y) => y.finishTime - x.finishTime);
            this.orders.splice(20, this.orders.length);
            this.changeDetectorRef.detectChanges();
            this.scrollableContainer.childrenChanged();
        })
    }
    private ngOnDestroy() {
        this.subscribetion.unsubscribe()
    }
    private trackByOrderId(index: number, order: Order) {
        return order.id;
    }
}
